/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2014-2015 Umeng, Inc
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package com.umeng.comm.ui.imagepicker.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

import com.qianxx.base.utils.SPUtil;
import com.qianxx.base.utils.ScreenUtils;
import com.umeng.comm.core.beans.CommUser;
import com.umeng.comm.core.beans.Topic;
import com.umeng.comm.core.imageloader.ImgDisplayOption;
import com.umeng.comm.core.impl.CommunitySDKImpl;
import com.umeng.comm.core.login.LoginListener;
import com.umeng.comm.core.sdkmanager.ImageLoaderManager;
import com.umeng.comm.core.utils.CommonUtils;
import com.umeng.comm.core.utils.ResFinder;
import com.umeng.comm.core.utils.ResFinder.ResType;
import com.umeng.comm.ui.imagepicker.adapters.viewholders.RecommendTopicViewHolder;
import com.umeng.comm.ui.imagepicker.listener.TopicToTopicDetail;
import com.umeng.community.example.Utils.ThirdLoginRequestUtils;


/**
 * 推荐话题的Adapter
 */
public class RecommendTopicAdapter extends BackupAdapter<Topic, RecommendTopicViewHolder> {

    protected static String mFeedsStr = "";
    protected static String mFansStr = "";
    protected static final String DIVIDER = " / ";

    protected FollowListener<Topic> mListener;
    protected boolean isFromFindPage = false;// 是否来自于发现页面。对于来自发现页面需要单独处理，

    protected int mTopicColor = 0;
    protected int mTopicIcon = 0;
    protected TopicToTopicDetail ttt;
    //是否是关注页面
    private boolean isFocus = false;

    //是否是自己
    private boolean isMe = false;

    public boolean isFocus() {
        return isFocus;
    }

    public void setIsFocus(boolean isFocus) {
        this.isFocus = isFocus;
    }

    public void setIsMe(boolean isMe) {
        this.isMe = isMe;
    }

    /**
     * 推荐话题的显示样式跟推荐用户的样式相同
     *
     * @param context
     */
    public RecommendTopicAdapter(Context context) {
        super(context);
        mTopicColor = ResFinder.getColor("umeng_comm_text_topic_light_color");
        mFeedsStr = ResFinder.getString("umeng_comm_feeds_num");
        mFansStr = ResFinder.getString("umeng_comm_fans_num");
        mTopicColor = ResFinder.getColor("umeng_topic_title");
        mTopicIcon = ResFinder.getResourceId(ResType.DRAWABLE,
                "umeng_comm_topic_icon");
    }

    public void setTtt(TopicToTopicDetail ttt) {
        this.ttt = ttt;
    }

    @Override
    protected RecommendTopicViewHolder createViewHolder() {
        RecommendTopicViewHolder recommendTopicViewHolder = new RecommendTopicViewHolder();
        recommendTopicViewHolder.setIsFocus(isFocus);
        return recommendTopicViewHolder;
    }

    @Override
    protected void setItemData(int position, final RecommendTopicViewHolder viewHolder, View rootView) {
        final Topic topic = getItem(position);
        viewHolder.mUserNameTextView.setText(topic.name);
        viewHolder.mUserNameTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
//        viewHolder.mUserNameTextView.setTextColor(ResFinder.getColor("umeng_comm_title"));
        ImgDisplayOption option = ImgDisplayOption.getCommonDisplayOption(); // 使用默认Option即可，会根据图片大小适当压缩
        option.mLoadingResId = comm.umeng.com.umeng_community_library_project.R.drawable.icon_pic_big;
        option.mLoadFailedResId = comm.umeng.com.umeng_community_library_project.R.drawable.icon_pic_big;
        if (TextUtils.isEmpty(topic.icon) || topic.icon.equals("null")) {
            viewHolder.mImageView.setImageUrl(topic.imageItems.get(0).thumbnail, ImgDisplayOption.getTopicIconOption());
//            viewHolder.mImageView.setImageResource(mTopicIcon);
            //如果是发现页面时，将图片的控件转变成Imageview
//            viewHolder.mIcon.setImageResource(mTopicIcon);
            if (topic.imageItems.size() > 0) {
                ImageLoaderManager.getInstance().getCurrentSDK().displayImage(topic.imageItems.get(0).originImageUrl, viewHolder.mIcon, option);
//                Log.e("com.draw", "img = " + topic.imageItems.get(0).originImageUrl);
            }
        } else {
            viewHolder.mImageView.setImageUrl(topic.icon, ImgDisplayOption.getTopicIconOption());
            //如果是发现页面时，将图片的控件转变成Imageview
            if (topic.imageItems.size() > 0) {
                ImageLoaderManager.getInstance().getCurrentSDK().displayImage(topic.imageItems.get(0).originImageUrl, viewHolder.mIcon, option);
//                Log.e("com.draw", "img = " + topic.imageItems.get(0).originImageUrl);
            }
        }
        setBannerHeight(viewHolder.mIcon);
//        viewHolder.mGenderImageView.setVisibility(View.GONE);
        viewHolder.mMsgFansTextView.setText(buildMsgFansStr(topic));

        if (isMe) {
            viewHolder.mToggleButton.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mToggleButton.setVisibility(View.GONE);
        }
        setToggleButtonStatusAndEvent(viewHolder, topic);

//        if (isFromFindPage) {
//            viewHolder.mUserNameTextView.setTextColor(ResFinder.getColor("umeng_comm_title"));
        rootView.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                gotoTopicDetailPage(topic);
            }
        });
    }
    // 设置padding
    // setupItemViewPadding(viewHolder);
//    }

    // protected void setupItemViewPadding(ActiveUserViewHolder viewHolder) {
    // int left = viewHolder.mView.getLeft() + DeviceUtils.dp2px(mContext, 4);
    // int top = viewHolder.mView.getLeft();
    // int right = viewHolder.mView.getLeft();
    // int bottom = viewHolder.mView.getLeft();
    // viewHolder.mView.setPadding(left, top, right, bottom);
    // }

    /**
     * 构建feed数量、粉丝数量的字符串
     *
     * @param topic
     * @return
     */
    protected String buildMsgFansStr(Topic topic) {
        StringBuilder builder = new StringBuilder(mFeedsStr);
        builder.append(topic.feedCount);
        builder.append(DIVIDER).append(mFansStr);
        builder.append(topic.fansCount);
        return builder.toString();
    }

    /**
     * 跳转到话题详情页面</br>
     *
     * @param topic
     */
    //// TODO: 16/1/19 反射跳转到不同界面
    protected void gotoTopicDetailPage(Topic topic) {
        if (ttt != null) {
            ttt.gotoTopicDetail(topic);
        }
//        Intent intent = new Intent();
//        ComponentName componentName = new ComponentName(mContext, TopicDetailActivity.class);
//        intent.setComponent(componentName);
//        intent.putExtra(Constants.TAG_TOPIC, topic);
//        ((Activity) mContext).startActivity(intent);
    }

    public void setFollowListener(FollowListener<Topic> listener) {
        this.mListener = listener;
    }

    /**
     * 设置是否来自于发送页面</br>
     *
     * @param fromFind
     */
    public void setFromFindPage(boolean fromFind) {
        isFromFindPage = fromFind;
    }

    private void setToggleButtonStatusAndEvent(final RecommendTopicViewHolder viewHolder,
                                               final Topic topic) {
        viewHolder.mToggleButton.setChecked(topic.isFocused);
//        viewHolder.mToggleButton.setOnClickListener(new Listeners.LoginOnViewClickListener() {
//
//            @Override
//            protected void doAfterLogin(View v) {
//                viewHolder.mToggleButton.setClickable(true);
//                mListener.onFollowOrUnFollow(topic, viewHolder.mToggleButton,
//                        viewHolder.mToggleButton.isChecked());
//            }
//        });
        viewHolder.mToggleButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(SPUtil.getInstance().getToken()) || !CommonUtils.isLogin(mContext)) {
                    CommunitySDKImpl.getInstance().login(mContext, new LoginListener() {
                        @Override
                        public void onStart() {

                        }

                        @Override
                        public void onComplete(int stCode, CommUser userInfo) {
                            if (stCode == 0) {
                                ThirdLoginRequestUtils.getInstance(mContext).ThirdLogin(userInfo);
                                viewHolder.mToggleButton.setClickable(true);
                                mListener.onFollowOrUnFollow(topic, viewHolder.mToggleButton,
                                        viewHolder.mToggleButton.isChecked());
                            }
                        }
                    });
                } else {
                    viewHolder.mToggleButton.setClickable(true);
                    mListener.onFollowOrUnFollow(topic, viewHolder.mToggleButton,
                            viewHolder.mToggleButton.isChecked());
                }
            }
        });
    }

    public static interface FollowListener<T> {
        public void onFollowOrUnFollow(T t, ToggleButton toggleButton, boolean isFollow);
    }

    /**
     * 设置图片的高度，让宽：高=1.8：1
     */
    private int setBannerHeight(View layout) {
        int screenWitdh = ScreenUtils.getScreenWitdh(mContext);
        int width = screenWitdh;
        int height = (int) (width / 1.8);
        if (isFocus) {
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,
                    height);
            layout.setLayoutParams(params);
        } else {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width,
                    height);
            layout.setLayoutParams(params);
        }

        return height;
    }

}
